package com.chexiehui.jc.widget;

import android.content.Context;
import android.support.annotation.NonNull;

import com.chexiehui.jc.R;
import com.chexiehui.jc.adapter.ChoosePriceGridAdapter;
import com.chexiehui.jc.adapter.ChooseTypeGridAdapter;
import com.chexiehui.jc.entity.DictInfo;
import com.chexiehui.jc.entity.PriceConditionInfo;
import com.lxj.xpopup.impl.PartShadowPopupView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class CustomChooseCarTypePopup extends PartShadowPopupView {

    @BindView(R.id.nsgv_type_grid)
    NonSlidingGridView nsgvTypeGrid;
    private Unbinder mUnBinder;
    private String chooseId = "";
    private List<DictInfo> typeList;
    private ChooseTypeGridAdapter typeAdapter;
    private ICustomChooseCarTypeListener listener;

    public CustomChooseCarTypePopup(@NonNull Context context) {
        super(context);
        typeList = new ArrayList<>();
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layput_custom_choose_car_type_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        mUnBinder = ButterKnife.bind(this);
        nsgvTypeGrid.setAdapter(typeAdapter = new ChooseTypeGridAdapter(getContext(), typeList));
        nsgvTypeGrid.setOnItemClickListener((parent, view, position, id) -> {
            if (typeAdapter.getChoosePosition() == position) {
                return;
            }
            typeAdapter.changeState(position);
            dismiss();
            if (listener != null) {
                listener.onChooseType(typeList.get(position).getKey());
            }
        });
        typeAdapter.changeState(-1);
    }

    public void initTypeModel(String chooseTypeId, List<DictInfo> list) {
        if(chooseTypeId != null ) chooseId = chooseTypeId;
        if(list != null){
            typeList.clear();
            typeList.addAll(list);
        }
    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
        setGridInfo();
    }

    public void setListener(ICustomChooseCarTypeListener listener) {
        this.listener = listener;
    }

    private void setGridInfo() {
        typeAdapter.changeState(-1);
        for (int i = 0; i < typeList.size(); i++) {
            if (chooseId.equals(typeList.get(i).getKey())) {
                typeAdapter.changeState(i);
                break;
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mUnBinder != null) {
            mUnBinder.unbind();
        }
    }

    public interface ICustomChooseCarTypeListener {
        void onChooseType(String typeId);
    }

}
